package com.cloudcentral.entity.vo;

import com.cloudcentral.entity.Menu;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by Administrator
 * 2023/11/2 16:35
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Schema(description = "菜单树状数据")
public class MenuTreeVO {
  @Schema(description = "ID")
  private Long id;
  @Schema(description = "上级ID")
  private Long parentId;
  @Schema(description = "名称")
  private String label;
  @Schema(description = "路径")
  private String path;
  @Schema(description = "图标")
  private String icon;
  @Schema(description = "序号")
  private Long idx;
  @Schema(description = "子菜单")
  private List<MenuTreeVO> children;

  public static List<MenuTreeVO> assemble(List<Menu> list, Long parentId) {
    return list.stream().filter(item -> parentId == null ? item.getParentId() == null : parentId.equals(item.getParentId()))
        .map(item -> MenuTreeVO.builder()
            .id(item.getId())
            .parentId(item.getParentId())
            .label(item.getLabel())
            .path(item.getPath())
            .icon(item.getIcon())
            .idx(item.getIdx())
            .children(assemble(list, item.getId()))
            .build()).collect(Collectors.toList());
  }
}
